﻿#region Using directives

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using MCMLXVII.Core.Common;
using System.ServiceModel;
#endregion

namespace MCMLXVII.Client.Common.Session
{
    partial class frmLogon : Form
    {
        private int TryCounter = 0;
        private bool Filling = false;
        private string _ToolName = "";
        private string _ToolVersion = "";
        
        public frmLogon()
        {
            InitializeComponent();
            Filling = true;
            
            cmbServers.Items.Clear();
            /*foreach (ServerInfo si in ToolSession.UserConnections)
            {
                cmbServers.Items.Add(si.ServerName);
            }*/
            if (cmbServers.Items.Count > 0)
                cmbServers.SelectedIndex = 0;
            else
            {
                txtUserName.Text = "";
                txtPassword.Text = "";
                chkSavepassword.Checked=false;
                chkSavepassword.Enabled=false;
                chkAutoConnect.Checked = false;
            }
            Filling = false;

        }

        public frmLogon(string ToolName)
        {
            InitializeComponent();
            Filling = true;
            _ToolName = ToolName;


            cmbServers.Items.Clear();
            /*foreach (ServerInfo si in ToolSession.UserConnections)
            {
                cmbServers.Items.Add(si.ServerName);
            }
            if (DefaultServerInfo.IntegratedLogon)
            {
                optLogonIntegrated.Checked = true;
                txtUserName.Text = "";
                txtPassword.Text = "";
                txtUserName.Enabled = false;
                txtPassword.Enabled = false;
                lblPassword.Enabled = false;
                lblUsername.Enabled = false;
                chkSavepassword.Checked=false;
                chkSavepassword.Enabled=false;
            }
            else
            {
                optLogonStandard.Checked = true;
                txtUserName.Text = DefaultServerInfo.UserName ;
                txtPassword.Text = DefaultServerInfo.Password;
                txtUserName.Enabled = true;
                txtPassword.Enabled = true;
                lblPassword.Enabled = true;
                lblUsername.Enabled = true;
                chkSavepassword.Checked=DefaultServerInfo.SavePassword;
                chkSavepassword.Enabled=true;

            }*/
            //chkAutoConnect.Checked = (ToolSession.AutomaticallyConnectTo != "");
            Filling = false;
        }
        private void frmLogon_Load(object sender, EventArgs e)
        {
        
        }

        private void optLogon2_CheckedChanged(object sender, EventArgs e)
        {
        
        }

        private void optLogonIntegrated_CheckedChanged(object sender, EventArgs e)
        {
            if (!Filling)
            {
                if (optLogonIntegrated.Checked)
                {
                    txtUserName.Text = "";
                    txtPassword.Text = "";
                    chkSavepassword.Checked = false;
                    lblUsername.Enabled = false;
                    lblPassword.Enabled = false;
                    txtUserName.Enabled = false;
                    txtPassword.Enabled = false;
                    chkSavepassword.Enabled = false;
                }
                else
                {
                    lblUsername.Enabled = true;
                    lblPassword.Enabled = true;
                    txtUserName.Enabled = true;
                    txtPassword.Enabled = true;
                    chkSavepassword.Enabled = true;
                    txtUserName.Focus();

                }
            }
        }

        private void optLogonStandard_CheckedChanged(object sender, EventArgs e)
        {
            if (!Filling)
            {
                if (!optLogonStandard.Checked)
                {
                    txtUserName.Text = "";
                    txtPassword.Text = "";
                    chkSavepassword.Checked = false;
                    lblUsername.Enabled = false;
                    lblPassword.Enabled = false;
                    txtUserName.Enabled = false;
                    txtPassword.Enabled = false;
                    chkSavepassword.Enabled = false;
                }
                else
                {
                    lblUsername.Enabled = true;
                    lblPassword.Enabled = true;
                    txtUserName.Enabled = true;
                    txtPassword.Enabled = true;
                    chkSavepassword.Enabled = true;
                    txtUserName.Focus();

                }
            }

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
         /*   if (optLogonIntegrated.Checked)
            {
                si.IntegratedLogon = true;
                si.UserName = "";
                si.Password = "";
                si.SavePassword = false;
            }
            else
            {
                si.IntegratedLogon = false;
                si.UserName = txtUserName.Text ;
                si.Password = txtPassword.Text ;
                si.SavePassword = chkSavepassword.Checked;
            }*/
            string res = ToolSession.ConnectToServer(_ToolName);
            if (res == "OK")
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show(ResourcesTools.RetornaLocPar(Properties.Resources.UnableToLogon, res));
                TryCounter++;
                if (TryCounter > 3)
                {
                    MessageBox.Show(Properties.Resources.UnableToContinue);
                    this.DialogResult = DialogResult.Abort;
                    this.Close();
                }
            }

        }
    }
}